package com.hp.team.bookstore.web.profile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.hp.team.bookstore.model.profile.Address;
import com.hp.team.bookstore.model.profile.CreditCard;
import com.hp.team.bookstore.model.profile.CreditCardType;
import com.hp.team.bookstore.model.profile.Customer;
import com.hp.team.bookstore.model.profile.Gender;
import com.hp.team.bookstore.model.profile.Telephone;
import com.hp.team.bookstore.service.profile.ProfileService;
import com.hp.team.bookstore.web.IPageUrlConstants;

@Controller
public class CustomerController implements IPageUrlConstants {

	@Autowired
	private ProfileService profileService;

	@RequestMapping(value = "/customer_new")
	public String addNewCustomer(ModelMap model) {
		return PAGE_CUSTOMER_NEW;
	}

	@RequestMapping(value = "/customer_add")
	public String addNewCustomer(ModelMap model, HttpSession session,  @RequestParam(required = false) String buildingNumber,
			@RequestParam(required = false) String street, @RequestParam(required = false) String city,
			@RequestParam(required = false) String state, @RequestParam(required = false) String zipCode,
			@RequestParam(required = false) String country, @RequestParam(required = false) String firstName,
			@RequestParam(required = false) String lastName, @RequestParam(required = false) String gender,
			@RequestParam(required = false) String countryCode, @RequestParam(required = false) String number,
			@RequestParam(required = false) String email, @RequestParam(required = false) String birthDate) {

		Address address = new Address();
		address.setBuildingNumber(buildingNumber);
		address.setCity(city);
		address.setState(state);
		address.setStreet(street);
		address.setZipCode(zipCode);
		address.setCountry(country);

		Telephone telephone = new Telephone();
		telephone.setCountryCode(countryCode);
		telephone.setNumber(number);

		Date date = new Date();

		try {
			date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(birthDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		Customer newCustomer = new Customer();
		newCustomer.setAddress(address);
		newCustomer.setFirstName(firstName);
		newCustomer.setLastName(lastName);
		newCustomer.setGender(Gender.valueOf(gender));
		newCustomer.setTelephone(telephone);
		newCustomer.setEmail(email);
		newCustomer.setBirthDate(calendar);

		model.addAttribute("customer", newCustomer);
		model.addAttribute("updateOperation", "true");
		model.addAttribute("enableNextStep", "true");
		model.addAttribute("selectedCustomer", newCustomer);
		session.setAttribute("selectedCustomer", newCustomer);
		return PAGE_CUSTOMER_INFO;
	}

	@RequestMapping(value = "/customer_details")
	public String showCustomerDetails(@RequestParam String id, ModelMap model) {
		long customerId = Long.parseLong(id);

		Customer customer = profileService.getCustomer(customerId);
		model.addAttribute("customer", customer);

		return PAGE_CUSTOMER_DETAILS;
	}

	@RequestMapping(value = "/credit_card_add", method = RequestMethod.POST)
	public String addAndSelectNewCreditCard(ModelMap model, HttpSession session, @RequestParam(required = false) String nameOnCard,
			@RequestParam(required = false) long cardNumber, @RequestParam(required = false) int securityCode,
			@RequestParam(required = false) String type, @RequestParam(required = false) String expirationDate) {

		Date date = new Date();
		try {
			date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(expirationDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		CreditCard newCreditCard = new CreditCard();
		newCreditCard.setNameOnCard(nameOnCard);
		newCreditCard.setCardNumber(cardNumber);
		newCreditCard.setSecurityCode(securityCode);
		newCreditCard.setType(CreditCardType.valueOf(type));
		newCreditCard.setExpirationDate(calendar);

		model.addAttribute("selectedCreditCard", newCreditCard);
		session.setAttribute("selectedCreditCard", newCreditCard);
		model.addAttribute("selectCreditCardItem", "true");
		model.addAttribute("enableNextStep", "true");
		model.addAttribute("paymentType", "credit");
		Customer c = (Customer) session.getAttribute("selectedCustomer");
		model.addAttribute("customerCreditCards", profileService.getCreditCardByCustomerID(c.getId()));

		return PAGE_CREDIT_CARD_INFO;
	}
}
